﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Deployment.Data.UI.Controls.Deploy
{
    /// <summary>
    /// Interaction logic for DeployEditorControl.xaml
    /// </summary>
    public partial class DeployEditorControl : UserControl
    {
        public static readonly DependencyProperty DeploymentFilePathProperty =
            DependencyProperty.Register("DeploymentFilePathText",
                typeof(string),
                typeof(DeployEditorControl), null);

        public static readonly DependencyProperty HasUnsavedChangesProperty =
            DependencyProperty.Register("HasUnsavedChanges",
                typeof(bool),
                typeof(DeployEditorControl), null);

        public static readonly DependencyProperty EnvironmentsProperty =
            DependencyProperty.Register("Environments",
                typeof(Data.Environment.EnvironmentsFile),
                typeof(DeployEditorControl));

        public event DependencyPropertyChangedEventHandler PropertyChanged;

        private void SetValueDp(DependencyProperty property, object value)
        {
            SetValue(property, value);
            if (PropertyChanged != null)
                PropertyChanged(this, new DependencyPropertyChangedEventArgs());
        }

        public DeployEditorControl()
        {
            InitializeComponent();
            this.DataContext = new Deployment.Data.Deploy.DeploymentFile();
            HasUnsavedChanges = false;
        }

        public Data.Environment.EnvironmentsFile Environments
        {
            get { return (Data.Environment.EnvironmentsFile)GetValue(EnvironmentsProperty); }
            set { SetValueDp(EnvironmentsProperty, value); }
        }

        public string DeploymentFilePathText
        {
            get { return (string)GetValue(DeploymentFilePathProperty); }
            set { SetValueDp(DeploymentFilePathProperty, value); }
        }

        public bool HasUnsavedChanges
        {
            get { return (bool)GetValue(HasUnsavedChangesProperty); }
            set { SetValueDp(HasUnsavedChangesProperty, value); }
        }

        public bool SaveChanges()
        {
            string filePath = DeploymentFilePathText;
            if (string.IsNullOrEmpty(filePath))
            {
                Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
                dlg.FileName = "Deployment.xml";
                dlg.DefaultExt = ".xml";
                dlg.Filter = "XML Document |*.xml|All Files|*.*";

                Nullable<bool> result = dlg.ShowDialog(Window.GetWindow(this));
                if (result == true)
                {
                    DeploymentFilePathText = dlg.FileName;
                }
            }

            if (!string.IsNullOrEmpty(DeploymentFilePathText))
            {
                Deployment.Data.Deploy.DeploymentFile file = DataContext as Deployment.Data.Deploy.DeploymentFile;
                if (file != null)
                {
                    file.SaveToFile(DeploymentFilePathText);
                }
                HasUnsavedChanges = false;
                return true;
            }
            else
            {
                MessageBox.Show(Window.GetWindow(this), "Save has been cancelled", "Operation Cancelled", MessageBoxButton.OK, MessageBoxImage.Warning);
                return false;
            }
        }

        private void SaveChanges_Click(object sender, RoutedEventArgs e)
        {
            HasUnsavedChanges = !SaveChanges();
        }

        private void EditableTaskList_Loaded(object sender, RoutedEventArgs e)
        {

        }
    }
}
